import 'package:boss/color_constant.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/widget/calendar/model/calendar_day.dart';
import 'package:boss/ui/widget/calendar/provider/calendar_provider.dart';
import 'package:boss/ui/widget/calendar/util/date_util.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class CalendarDayCell extends StatelessWidget {
  final CalendarDay data;
  final VoidCallback onTap;

  const CalendarDayCell({
    Key key,
    @required this.data,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer<CalendarProvider>(
      builder: (context, provider, child) {
        bool isStartDateTime = false;
        bool isEndDateTime = false;
        bool isBetween = false;
        bool isSaturday = false;
        bool isSunday = false;
        bool isSelectedStarEnd =
            provider.startDateTime != null && provider.endDateTime != null;
        bool isFirstDayInMonth = false;
        bool isLastDayInMonth = false;

        if (data.dateTime != null && provider.startDateTime != null) {
          isSaturday = data.dateTime.weekday == 6;
          isSunday = data.dateTime.weekday == 7;
          isFirstDayInMonth = data.dateTime.day == 1;
          isLastDayInMonth =
              data.dateTime.day == DateUtils.getDaysInMonth(data.dateTime);

          isStartDateTime =
              DateUtils.sameDate(data.dateTime, provider.startDateTime);

          if (provider.endDateTime != null) {
            isEndDateTime =
                DateUtils.sameDate(data.dateTime, provider.endDateTime);

            isBetween = data.dateTime.isBefore(provider.endDateTime) &&
                data.dateTime.isAfter(provider.startDateTime);
          }
        }

        bool isRange = isStartDateTime || isEndDateTime;

        double radius = 66.w;

        return GestureDetector(
          onTap: onTap,
          child: Container(
            alignment: Alignment.center,
            decoration: BoxDecoration(
                color: isBetween || isRange && isSelectedStarEnd
                    ? ColorConstant.f7184f5
                    : Colors.transparent,
                borderRadius: isSelectedStarEnd
                    ? BorderRadius.only(
                  topLeft:
                  isStartDateTime || isSunday || isFirstDayInMonth
                      ? Radius.circular(radius)
                      : Radius.zero,
                  bottomLeft:
                  isStartDateTime || isSunday || isFirstDayInMonth
                      ? Radius.circular(radius)
                      : Radius.zero,
                  topRight:
                  isEndDateTime || isSaturday || isLastDayInMonth
                      ? Radius.circular(radius)
                      : Radius.zero,
                  bottomRight:
                  isEndDateTime || isSaturday || isLastDayInMonth
                      ? Radius.circular(radius)
                      : Radius.zero,
                )
                    : BorderRadius.zero),
            child: Container(
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: isRange ? ColorConstant.ff5770f6 : Colors.transparent,
                ),
                alignment: Alignment.center,
                child: Text(
                  '${data.day}',
                  style: isRange || isBetween
                      ? TextStyleConstant().white_28
                      : data.isDisabled
                      ? TextStyleConstant().normal_4_28
                      : TextStyleConstant().normal_1_28,
                )
            ),
          ),
        );
      },
    );
  }
}
